#include "gifgraphiccontrolextension.h"

namespace WingGif {

GifGraphicControlExtension::GifGraphicControlExtension(QObject *parent)
    : GifExtension(parent) {}

int GifGraphicControlExtension::blockSize() { return _blockSize; }

int GifGraphicControlExtension::disposalMethod() { return _disposalMethod; }

bool GifGraphicControlExtension::userInput() { return _userInput; }

bool GifGraphicControlExtension::hasTransparency() { return _hasTransparency; }

int GifGraphicControlExtension::delay() { return _delay; }

int GifGraphicControlExtension::transparencyIndex() {
  return _transparencyIndex;
}

bool GifGraphicControlExtension::read(QIODevice *stream,
                                      QList<GifExtension *> controlExtensions) {
  Q_UNUSED(controlExtensions)
  // Note: at this point, the label (0xF9) has already been read
  auto bytes = stream->read(6);
  _blockSize = bytes[0]; // should always be 4

  if (_blockSize != 4)
    return false;

  auto packedFields = bytes[1];
  _disposalMethod = (packedFields & 0x1C) >> 2;
  _userInput = (packedFields & 0x02) != 0;
  _hasTransparency = (packedFields & 0x01) != 0;
  _delay = (*reinterpret_cast<quint16 *>(bytes.data() + 2)) * 10;
  _transparencyIndex = bytes[4];

  return true;
}

GifBlockKind GifGraphicControlExtension::kind() {
  return GifBlockKind::Control;
}

} // namespace WingGif
